package com.wuwei.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.common.constant.ProductConstant;
import com.wuwei.common.utils.Constant;
import com.wuwei.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.wuwei.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.wuwei.gulimall.product.entity.AttrEntity;
import com.wuwei.gulimall.product.service.AttrAttrgroupRelationService;
import com.wuwei.gulimall.product.service.AttrService;
import com.wuwei.gulimall.product.vo.AttrGroupRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwei.common.utils.PageUtils;
import com.wuwei.common.utils.Query;

import com.wuwei.gulimall.product.dao.AttrGroupDao;
import com.wuwei.gulimall.product.entity.AttrGroupEntity;
import com.wuwei.gulimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取分类属性分组
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPageAttribute(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<AttrGroupEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(key)){
            wrapper.and(a->{
                a.eq(AttrGroupEntity::getAttrGroupId,key).or().like(AttrGroupEntity::getAttrGroupName,key);
            });
        }
        if (catelogId==0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }
        wrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                wrapper);
        return new PageUtils(page);
    }

    /**
     * 获取属性分组没有关联的其他属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @Override
    public PageUtils getNoRelation(Long attrgroupId, Map<String, Object> params) {
        //当前分组只能关联自己所属分类里的所有属性
        AttrGroupEntity attrGroupEntity = this.getById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId, catelogId));
        //当前分组只能关联别的分组没有引用的属性
        //找到当前分类下的其他分组
        List<Long> attrIdList = attrEntities.stream().map(AttrEntity::getAttrId).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> attrGroupRelationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(!CollectionUtils.isEmpty(attrIdList),AttrAttrgroupRelationEntity::getAttrId, attrIdList).ne(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        //找到这些分组关联的属性
        List<Long> attrIds = attrGroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        //从当前分类的所有属性中移除这些属性
        LambdaQueryWrapper<AttrEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttrEntity::getCatelogId, catelogId)
                .notIn(!CollectionUtils.isEmpty(attrIds),AttrEntity::getAttrId, attrIds)
                .eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        //找出自身关联的属性
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<Long> attrList = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        //排除自身关联的属性
        queryWrapper.notIn(!CollectionUtils.isEmpty(attrList),AttrEntity::getAttrId, attrList);
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.and(a->{
                a.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
            });
        }
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }
    /**
     * 删除属性同时解除属性和分组的关系
     * @param attrGroupIds
     */
    @Transactional
    @Override
    public void removeAndUnRelated(List<Long> attrGroupIds) {
        this.removeByIds(attrGroupIds);
        //解除属性和分组的关系
        attrAttrgroupRelationDao.batchGroupDelete(attrGroupIds);
    }


}
